{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 中国与全球化智库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from stocker import Stocker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CCG Stocker Initialized. Data covers 2010-10-14 00:00:00 to 2016-03-02 00:00:00.\n"
     ]
    }
   ],
   "source": [
    "centerChinaGlobalization = Stocker('CCG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Ex-Dividend</th>\n",
       "      <th>Split Ratio</th>\n",
       "      <th>Adj. Open</th>\n",
       "      <th>Adj. High</th>\n",
       "      <th>Adj. Low</th>\n",
       "      <th>Adj. Close</th>\n",
       "      <th>Adj. Volume</th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>Daily Change</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2010-10-14</td>\n",
       "      <td>12.50</td>\n",
       "      <td>12.64</td>\n",
       "      <td>12.480</td>\n",
       "      <td>12.51</td>\n",
       "      <td>8250100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.677405</td>\n",
       "      <td>9.785792</td>\n",
       "      <td>9.661921</td>\n",
       "      <td>9.685147</td>\n",
       "      <td>8250100.0</td>\n",
       "      <td>2010-10-14</td>\n",
       "      <td>9.685147</td>\n",
       "      <td>0.007742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2010-10-15</td>\n",
       "      <td>12.56</td>\n",
       "      <td>12.85</td>\n",
       "      <td>12.500</td>\n",
       "      <td>12.58</td>\n",
       "      <td>2384900.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.723856</td>\n",
       "      <td>9.948372</td>\n",
       "      <td>9.677405</td>\n",
       "      <td>9.739340</td>\n",
       "      <td>2384900.0</td>\n",
       "      <td>2010-10-15</td>\n",
       "      <td>9.739340</td>\n",
       "      <td>0.015484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2010-10-18</td>\n",
       "      <td>12.64</td>\n",
       "      <td>12.77</td>\n",
       "      <td>12.520</td>\n",
       "      <td>12.70</td>\n",
       "      <td>407600.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.785792</td>\n",
       "      <td>9.886437</td>\n",
       "      <td>9.692888</td>\n",
       "      <td>9.832243</td>\n",
       "      <td>407600.0</td>\n",
       "      <td>2010-10-18</td>\n",
       "      <td>9.832243</td>\n",
       "      <td>0.046452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2010-10-19</td>\n",
       "      <td>12.70</td>\n",
       "      <td>12.70</td>\n",
       "      <td>12.500</td>\n",
       "      <td>12.53</td>\n",
       "      <td>575900.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.832243</td>\n",
       "      <td>9.832243</td>\n",
       "      <td>9.677405</td>\n",
       "      <td>9.700630</td>\n",
       "      <td>575900.0</td>\n",
       "      <td>2010-10-19</td>\n",
       "      <td>9.700630</td>\n",
       "      <td>-0.131613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2010-10-20</td>\n",
       "      <td>12.55</td>\n",
       "      <td>12.80</td>\n",
       "      <td>12.495</td>\n",
       "      <td>12.64</td>\n",
       "      <td>1259400.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.716114</td>\n",
       "      <td>9.909662</td>\n",
       "      <td>9.673534</td>\n",
       "      <td>9.785792</td>\n",
       "      <td>1259400.0</td>\n",
       "      <td>2010-10-20</td>\n",
       "      <td>9.785792</td>\n",
       "      <td>0.069677</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date   Open   High     Low  Close     Volume  Ex-Dividend  \\\n",
       "0 2010-10-14  12.50  12.64  12.480  12.51  8250100.0          0.0   \n",
       "1 2010-10-15  12.56  12.85  12.500  12.58  2384900.0          0.0   \n",
       "2 2010-10-18  12.64  12.77  12.520  12.70   407600.0          0.0   \n",
       "3 2010-10-19  12.70  12.70  12.500  12.53   575900.0          0.0   \n",
       "4 2010-10-20  12.55  12.80  12.495  12.64  1259400.0          0.0   \n",
       "\n",
       "   Split Ratio  Adj. Open  Adj. High  Adj. Low  Adj. Close  Adj. Volume  \\\n",
       "0          1.0   9.677405   9.785792  9.661921    9.685147    8250100.0   \n",
       "1          1.0   9.723856   9.948372  9.677405    9.739340    2384900.0   \n",
       "2          1.0   9.785792   9.886437  9.692888    9.832243     407600.0   \n",
       "3          1.0   9.832243   9.832243  9.677405    9.700630     575900.0   \n",
       "4          1.0   9.716114   9.909662  9.673534    9.785792    1259400.0   \n",
       "\n",
       "          ds         y  Daily Change  \n",
       "0 2010-10-14  9.685147      0.007742  \n",
       "1 2010-10-15  9.739340      0.015484  \n",
       "2 2010-10-18  9.832243      0.046452  \n",
       "3 2010-10-19  9.700630     -0.131613  \n",
       "4 2010-10-20  9.785792      0.069677  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_history = centerChinaGlobalization.stock\n",
    "stock_history.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg1.txt','w')\n",
    "f.write(str(stock_history.head()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Adj. Close'],\n",
       " 12.66954696477888,\n",
       " Timestamp('2013-04-08 00:00:00'),\n",
       " 4.77,\n",
       " Timestamp('2015-09-10 00:00:00'),\n",
       " 7.0,\n",
       " Timestamp('2016-03-02 00:00:00'))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.plot_stock()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg2.txt','w')\n",
    "f.write(str(centerChinaGlobalization.plot_stock()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Daily Change'],\n",
       " 1.2445714285714278,\n",
       " Timestamp('2014-11-04 00:00:00'),\n",
       " -0.6186034127506321,\n",
       " Timestamp('2011-10-05 00:00:00'),\n",
       " 0.0,\n",
       " Timestamp('2016-03-02 00:00:00'))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.plot_stock(start_date = '2011-01-03', end_date = '2016-01-16', \n",
    "                     stats = ['Daily Change'], plot_type='pct')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg3.txt','w')\n",
    "f.write(str(centerChinaGlobalization.plot_stock(start_date = '2011-01-01', end_date = '2016-01-16', \n",
    "                     stats = ['Daily Change'], plot_type='pct')))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['Adj. Volume'],\n",
       " 10957600.0,\n",
       " Timestamp('2013-03-01 00:00:00'),\n",
       " 44800.0,\n",
       " Timestamp('2011-09-26 00:00:00'),\n",
       " 815848.0,\n",
       " Timestamp('2016-03-02 00:00:00'))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.plot_stock(start_date = '2011-01-03', end_date = '2016-01-16', \n",
    "                     stats = ['Adj. Volume'], plot_type='pct')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg4.txt','w')\n",
    "f.write(str(centerChinaGlobalization.plot_stock(start_date = '2011-01-03', end_date = '2016-01-16', \n",
    "                     stats = ['Adj. Volume'], plot_type='pct')))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('CCG',\n",
       " Timestamp('2010-10-15 00:00:00'),\n",
       " Timestamp('2013-04-30 00:00:00'),\n",
       " 100,\n",
       " 237.02047111340062)"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.buy_and_hold(start_date='2010-10-15', end_date='2013-04-30', nshares=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg5.txt','w')\n",
    "f.write(str(centerChinaGlobalization.buy_and_hold(start_date='2010-10-15', end_date='2013-04-30', nshares=100)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('CCG',\n",
       " Timestamp('2011-02-10 00:00:00'),\n",
       " Timestamp('2016-03-01 00:00:00'),\n",
       " 100,\n",
       " -345.79061651474507)"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.buy_and_hold(start_date='2011-02-10', end_date='2016-03-01', nshares=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('ccg6.txt','w')\n",
    "f.write(str(centerChinaGlobalization.buy_and_hold(start_date='2011-02-10', end_date='2016-03-01', nshares=100)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Adj. Close</th>\n",
       "      <th>delta</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>507</th>\n",
       "      <td>2015-03-09</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>-1.389158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410</th>\n",
       "      <td>2014-10-16</td>\n",
       "      <td>6.430286</td>\n",
       "      <td>1.292803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338</th>\n",
       "      <td>2014-07-07</td>\n",
       "      <td>8.320805</td>\n",
       "      <td>-1.172987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>579</th>\n",
       "      <td>2015-06-19</td>\n",
       "      <td>5.810000</td>\n",
       "      <td>1.060512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>265</th>\n",
       "      <td>2014-03-21</td>\n",
       "      <td>8.121064</td>\n",
       "      <td>0.978358</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Date  Adj. Close     delta\n",
       "507 2015-03-09    7.700000 -1.389158\n",
       "410 2014-10-16    6.430286  1.292803\n",
       "338 2014-07-07    8.320805 -1.172987\n",
       "579 2015-06-19    5.810000  1.060512\n",
       "265 2014-03-21    8.121064  0.978358"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centerChinaGlobalization.changepoint_date_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "f = open('ccg7.txt','w')\n",
    "f.write(str(centerChinaGlobalization.changepoint_date_analysis()))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "model, future = centerChinaGlobalization.create_prophet_model(days=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Downloads\\Anaconda\\lib\\site-packages\\pystan\\misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  elif np.issubdtype(np.asarray(v).dtype, float):\n"
     ]
    }
   ],
   "source": [
    "f = open('ccg8.txt','w')\n",
    "f.write(str(centerChinaGlobalization.create_prophet_model(days=30)))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
